#!/bin/bash
# chkconfig: 12345 07 91
# description: This calls the HMC restore scripts (prior to network set up)
#

### BEGIN INIT INFO
# Provides: hmcRestore
# Required-Start: $local_fs
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Provide HMC Restore
# Description: Restore Upgrade and Critical Data
### END INIT INFO
# Source function library.
. /etc/rc.status
rc_reset
# Critical error messages. Keep them under 80 chars per line
ARCHIVE_ERR_MSG_0="A critical error occurred during the recovery of archive data."
ARCHIVE_ERR_MSG_1="The system may be unstable. It is recommended the HMC be"
ARCHIVE_ERR_MSG_2="re-installed and manually reconfigured.\n"
ARCHIVE_ERR_MSG_3="Contact your service representative to determine the severity"
ARCHIVE_ERR_MSG_4="of this condition if you choose to proceed. Press <Enter> to"
ARCHIVE_ERR_MSG_5="continue the boot sequence or press 'R', then <Enter> to"
ARCHIVE_ERR_MSG_6="re-install the HMC.\n"

REINSTALL_MSG="Insert the HMC recovery CD and press <Enter> to continue with re-installation."

NO_RESTORE_MSG_0="A non-critical error was encountered while recovering archive data."
NO_RESTORE_MSG_1="No archive files have been restored. Contact your service"
NO_RESTORE_MSG_2="representative for further analysis. Press <Enter> to continue"
NO_RESTORE_MSG_3="the boot sequence."

case "$1" in
    start)
 
        # Logging information
        LOGDIR=/var/hsc/log
        FINALLOG=$LOGDIR/hmcRestore.log
        LOG=$LOGDIR/hmcRestore.tmp
        LOG_ERROR_LOG=/tmp/hmcRestore.log
        
        # Check if the directory for the log file exists.
        if [ ! -d $LOGDIR ]; then
            echo "=================================================================" > $LOG_ERROR_LOG
            echo "HMC restore script processing log for `date`." >> $LOG_ERROR_LOG
            echo "HMC log directory, <$LOGDIR>, does not exist. Program exiting" >> $LOG_ERROR_LOG
            exit 1
        fi
        
        
        #-----------------------------------------------------------------------
        # Something to the screen while booting
        #-----------------------------------------------------------------------
        echo "Processing HMC archive files ..."
        
        
        #-----------------------------------------------------------------------
        # Start log to HMC restore actions.
        #-----------------------------------------------------------------------
        if [ ! -f $LOG ]; then
           echo "--------- start log --------" > $LOG
        else
           echo "--------- log resumed after reboot --------" >> $LOG
        fi
        echo -e "HMC restore processing log for `date`.\n" >> $LOG
        
        
        #-----------------------------------------------------------------------
        # Call the actual data restore processes
        #-----------------------------------------------------------------------
        /bin/sh /opt/hsc/bin/restore 2>/dev/null
        restoreRC=$?
        echo "system restore of backup critical data completed, rc=$restoreRC." >> $LOG
        
        case "$restoreRC" in 
            0) 
                 # Successful restore of backup data was done.  Force a warm 
                 # reboot since we do not know what, if any, critical files 
                 # may have been copied back onto the system that was just 
                 # re-loaded.
                 #
                 # An rc=0 indicated that an actual restore was done. Rc=12 
                 # states that we attampted to do a restore, but none was 
                 # necessary. In between rc's are various errors.
                 #
                 # Use 'reboot' since it's unclear whether or not doing a 
                 # shutdown will attempt a level switch and execution of 'kill' 
                 # scripts.
                 #
                 /bin/sync
                 /sbin/reboot -f
                 
                 rc_exit
                 ;;
             
            99) 
                 # This is bad. It indicates that during the restore of the 
                 # backup data there was a 'tar' error during processing.  The 
                 # condition *may* now exist on the system where we have 
                 # incompatible executables and libraries, but maybe not - no 
                 # way to tell.  Prompt user with continuation options.

                 echo -e "\n\n\n"
                 echo -e $ARCHIVE_ERR_MSG_0
                 echo -e $ARCHIVE_ERR_MSG_1
                 echo -e $ARCHIVE_ERR_MSG_2
                 echo -e $ARCHIVE_ERR_MSG_3
                 echo -e $ARCHIVE_ERR_MSG_4
                 echo -e $ARCHIVE_ERR_MSG_5
                 echo -e $ARCHIVE_ERR_MSG_6
             
                 # Hold processing until user acknowledges msg
                 # line is /dev/pts/2
                 line=$(tty)
                 while read input;
                 do
                      # <Enter> key appears to get us out of loop
                      break
                 done
             
                 #
                 # Users assumes all risks with continuing with the boot
                 # process. They have been warned of potential instability.
                 #
                 if [[ "$input" == "R" ||  "$input" == "r" ]]; then
                      #
                      # Prompt with additional message to re-install product
                      #
                      echo -e $REINSTALL_MSG
                  
                      # Hold processing until user acknowledges msg
                      line=$(tty)
                      while read input;
                      do
                           # <Enter> key appears to get us out of loop
                           break
                      done
                  
                      # Reboot assuming the recovery CD has been re-inserted
                      /sbin/reboot -f
             
                      rc_exit
                 else
                      # User has been warned.  Force the reboot
                      /bin/sync
                      /sbin/reboot -f
             
                      rc_exit
                 fi
                 ;;
             
            1|2|4|5|6|8|9|10|11|13|14|15) 
                 # Come to think of it now, we should indicate a non-critcial 
                 # error occurred ('mount' failure, media error, no data, etc.) 
                 # and no restore was done. Again, direct them to the error log 
                 # and present continuation options.

                 echo -e "\n\n\n"
                 echo -e $NO_RESTORE_MSG_0
                 echo -e $NO_RESTORE_MSG_1
                 echo -e $NO_RESTORE_MSG_2
                 echo -e $NO_RESTORE_MSG_3
                  
                 # Hold processing until user acknowledges msg
                 line=$(tty)
                 while read input;
                 do
                      # <Enter> key appears to get us out of loop
                      break
                 done
		 rc_status -v
                 ;;
            *) 
                 ;;
        esac

        
        # Now do the restore of upgrade data files, if required
        /bin/sh /opt/hsc/bin/restoreUpgradeFiles 2>/dev/null
        echo "system restore of upgrade files completed, rc = $?." >> $LOG
        
        
        #-----------------------------------------------------------------------
        # Now that the system data has been restored, check for a specific
        # migration release - to release 3.2.  Since the 'ccfw' userid is new
        # to this particular release, it will not have had a prior password/home
        # directory, etc. Re-add it's entries to /etc/passwd and /etc/shadow
        # files if it does not already exist.
        #-----------------------------------------------------------------------
        typeset -i pr1
        typeset -i pr2
        typeset -i pv
        typeset -i r1
        typeset -i r2
        typeset -i v
        pr1=0
        pr2=0
        pv=0

        # HMC command to get the current release/version        
#        x=`/bin/bash /opt/hsc/bin/hsc version`
#        
#        v=`echo "$x"|grep Version|cut -d ':' -f 2 | cut -d ' ' -f 2`
#        r1=`echo "$x"|grep Release|cut -d ':' -f 2 | cut -d ' ' -f 2 | cut -d '.' -f 1`
#        r2=`echo "$x"|grep Release|cut -d ':' -f 2 | cut -d ' ' -f 2 | cut -d '.' -f 2`
#        if [[ $pr1 -eq 0  ||  $r1 -gt $pr1 ]]; then
#             pr1=r1
#        fi
#        if [[ $pr2 -eq 0  ||  $r2 -gt $pr2 ]]; then
#             pr2=r2
#        fi 
#        if [[ $pv -eq 0  ||  $v -gt $pv ]]; then
#             pv=v
#        fi
#        echo "current HMC release/version is: release $pv, version $pr1.$pr2" >> $LOG
        

        #-----------------------------------------------------------------------
        # Regardless if the 'ccfw' userid existed before/after either restore,
        # there is the possibility that the opt/ccfw directory ownership will
        # be corrupt (different GID).
        #-----------------------------------------------------------------------
        chown -R root.ccfw /opt/ccfw
        
        
        #-----------------------------------------------------------------------
        # Migrate password
        #-----------------------------------------------------------------------
	JAVAPATH="/opt/IBMJava/jre/bin"
        export LD_LIBRARY_PATH=/usr/sbin/rsct/lib:/usr/lib:/opt/hsc/lib:$LD_LIBRARY_PATH
        export PATH=$JAVAPATH:$PATH

        x=sslite-us.zip:sslite-ex.zip:jcb.jar:images.jar:wsmextra.jar:jcchart.jar:jcswingsuite.jar:/usr/websm/codebase/taskguide:mm_snmp.jar:wsm.jar:habeans.jar:log.jar:
        for i in /usr/websm/codebase/pluginjars/*.jar
        do
           x=${x}pluginjars/`basename $i`:
        done

        if [ "${DEBUG_JARS_DIRECTORY}" != "" ] ; then
          if [ -d ${DEBUG_JARS_DIRECTORY} ] ; then
            for i in ${DEBUG_JARS_DIRECTORY}/*.jar
            do
               debug_jars=${debug_jars}:$i
            done
          fi
        fi

        export CUSTOM_CLASSPATH=${DEBUG_JARS_DIRECTORY}:${debug_jars}:/usr/websm/codebase/images:/usr/websm/codebase/pluginjars/hsc_${LANG}.jar:/usr/websm/codebase/pluginjars/wsmhscsecurity.jar:/usr/websm/codebase/pluginjars/hsc.jar:/usr/websm/codebase/pluginjars/hsc_bundles.jar:/opt/hsc/auiml/properties:/usr/websm/codebase:/opt/hsc/com/ibm/hsc/auiml/databeans/images:$x
        $JAVAPATH/java -cp $CUSTOM_CLASSPATH com.ibm.hsc.common.util.HscPasswordMgr hscroot
        if [ $? -eq 0 ]
        then
           rm -f /opt/hsc/data/passwords
        fi

        echo "completed hscroot password migration." >> $LOG

        #-----------------------------------------------------------------------
        # Reset the hostname since a change could have occurred after doing
        # a restore or restore of upgrade data
        #-----------------------------------------------------------------------
        HOSTNAME=`/bin/hostname`
        echo "current hostname is: $HOSTNAME." >> $LOG

        if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
           HOSTNAME=localhost
        fi

        # Reset the hostname
        echo "Resetting hostname to ${HOSTNAME}"
        hostname ${HOSTNAME}
        echo "new hostname is: $HOSTNAME." >> $LOG


        chmod 644 $LOG
        mv -f $LOG $FINALLOG
                ;;
        stop)
                ;;
        *)
                echo "Usage: hmcRestore {start|stop}"
                exit 1
                ;;
        esac
rc_exit

